package cn.wen.yinxiu.system.service.impl;

import cn.wen.yinxiu.common.convention.result.ResultEntity;
import cn.wen.yinxiu.common.enums.DelEnum;
import cn.wen.yinxiu.common.enums.StatusEnum;
import cn.wen.yinxiu.system.feign.FileFeignService;
import cn.wen.yinxiu.system.feign.WorksFeignService;
import cn.wen.yinxiu.system.vo.AddAdvertVO;
import cn.wen.yinxiu.system.vo.UpdateAdvertVO;
import com.google.gson.Gson;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wen.yinxiu.common.toolkit.PageUtils;
import cn.wen.yinxiu.common.toolkit.Query;

import cn.wen.yinxiu.system.dao.AdvertDao;
import cn.wen.yinxiu.system.entity.AdvertEntity;
import cn.wen.yinxiu.system.service.AdvertService;
import org.springframework.web.multipart.MultipartFile;

@Service("advertService")
public class AdvertServiceImpl extends ServiceImpl<AdvertDao, AdvertEntity> implements AdvertService {

    @Autowired
    private AdvertDao advertDao;

    @Autowired
    private FileFeignService fileFeignService;

    @Autowired
    private WorksFeignService worksFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AdvertEntity> page = this.page(
                new Query<AdvertEntity>().getPage(params),
                new QueryWrapper<AdvertEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean saveWorksAdvertInfo(MultipartFile[] imageList, String advertInfoJSON) {
        int res = 0;
        try {
            // 1.远程调用文件上传服务
            ResultEntity<String> imageListRes = fileFeignService.uploadCloudFile(Arrays.asList(imageList));
            String imagePathList = imageListRes.getData();
            // 2.解析对应的JSON数据
            Gson gson = new Gson();
            AddAdvertVO addAdvertVO = gson.fromJson(advertInfoJSON, AddAdvertVO.class);
            // 3.根据对应的数据格式进行解析
            // 3.补充相关信息 根据作品类型存储记录
            AdvertEntity target = new AdvertEntity();
            BeanUtils.copyProperties(addAdvertVO, target);
            target.setAdvertImages(imagePathList);
            target.setIsDeleted(DelEnum.NORMAL.code());
            target.setStatus(StatusEnum.NORMAL_USE.code());
            // 4.将作品数据存入数据库
            res = advertDao.insert(target);
            // 5. TODO 删除相对于缓存
            // 7.远程服务调用WorksService将当前作品ID存入到作品数据中
            worksFeignService.addWorksAdvert(addAdvertVO.getWorksId(), target.getId());
        } catch (Exception e) {
            // 如果抛出异常则会回滚事务 则需要删除文件  这个则调用
            // TODO 为了保证删除成功 则将删除操作发送到消息队列中实现
            e.printStackTrace();
        }
        return res > 0;
    }

    @Override
    public boolean deleteWorksAdvertInfo(Long worksId, Long advertId) {
        // 1.删除商品信息
        AdvertEntity advertEntity = new AdvertEntity();
        advertEntity.setId(advertId);
        advertEntity.setIsDeleted(DelEnum.DELETE.code());
        int res = advertDao.updateById(advertEntity);
        // 2.将商品ID从作品中移除
        worksFeignService.deleteWorksAdvertById(worksId).getData();
        return res > 0;
    }

    @Override
    public boolean updateWorksAdvertInfo(MultipartFile[] imageList, String advertInfoJSON) {
        AdvertEntity advertEntity = new AdvertEntity();
        if (imageList != null && imageList.length != 0) {
            // 1.远程调用文件上传服务
            ResultEntity<String> imageListRes = fileFeignService.uploadCloudFile(Arrays.asList(imageList));
            String imagePathList = imageListRes.getData();
            advertEntity.setAdvertImages(imagePathList);
        }
        Gson gson = new Gson();
        UpdateAdvertVO updateAdvertVO = gson.fromJson(advertInfoJSON, UpdateAdvertVO.class);
        BeanUtils.copyProperties(updateAdvertVO, advertEntity);
        boolean res = advertDao.updateById(advertEntity) > 0;
        // TODO 删除缓存
        return res;
    }

}